package com.onepower.core.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onepower.core.domain.request.MatchRefereeForm;
import com.onepower.core.domain.response.RefereeInfo;
import com.onepower.core.modules.repository.entity.MatchReferee;
import com.onepower.core.modules.repository.entity.SysUser;
import com.onepower.core.modules.repository.entity.SysUserRole;
import com.onepower.core.modules.repository.mapper.MatchRefereeMapper;
import com.onepower.core.modules.service.IMatchRefereeService;
import com.onepower.core.modules.service.ISysUserRoleService;
import com.onepower.core.modules.service.ISysUserService;
import com.onepower.core.utils.SecurityUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 比赛-场地-裁判关联表 服务实现类
 * </p>
 *
 * @author kin
 * @since 2018-08-06
 */
@Service
public class MatchRefereeServiceImpl extends ServiceImpl<MatchRefereeMapper, MatchReferee> implements IMatchRefereeService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Autowired
    HttpServletRequest request;

    @Override
    public List<RefereeInfo> getRefereeList(Integer matchId,Integer refereeId) {
        if (SecurityUtils.getCurrentUserId(request) == 8888 || SecurityUtils.getCurrentUserId(request) == 9999){
            return baseMapper.selectRefereeList(matchId,refereeId);
        }else {
            return baseMapper.selectRefereeList(matchId, SecurityUtils.getCurrentUserId(request));
        }

    }

    @Override
    public MatchReferee getReferee(Integer refereeId) {
        return this.getOne(new LambdaQueryWrapper<MatchReferee>().eq(MatchReferee::getRefereeId,refereeId));
    }

    @Transactional
    @Override
    public void updateRefereeLoginType(Integer id, boolean isLogin) {
        MatchReferee referee = baseMapper.selectById(id);
        if (isLogin) {
            referee.setIsLogin(1);
        } else {
            referee.setIsLogin(0);
        }
        baseMapper.updateById(referee);
        sysUserService.restPassword(referee.getRefereeId(), SecurityUtils.generatorPass());
    }

    @Transactional
    @Override
    public void restSingleLoginState(Integer id) {
        MatchReferee referee = baseMapper.selectById(id);
        referee.setIsLogin(0);
        baseMapper.updateById(referee);
    }

    @Transactional
    @Override
    public void save(MatchRefereeForm matchRefereeForm) {
        SysUser sysUser = new SysUser();
        sysUser.setNickName(matchRefereeForm.getRefereeName());
        sysUser.setMatchId(matchRefereeForm.getMatchId());
        sysUser.setAccount(matchRefereeForm.getAccount());
        sysUser.setPassword(SecurityUtils.generatorPass());
        sysUser.setCreateUserId(SecurityUtils.getCurrentUserId(request));
        sysUser.setExpiryDate(new Date());
        sysUserService.save(sysUser);

        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getId());
        sysUserRole.setRoleId(6);
        sysUserRoleService.save(sysUserRole);

        MatchReferee referee = null;
        if (matchRefereeForm.getMatchId() == null){
            referee = this.getOne(new QueryWrapper<MatchReferee>()
                    .eq("match_id", matchRefereeForm.getMatchId())
                    .eq("match_admin_id", SecurityUtils.getCurrentUserId(request))
                    .eq("referee_id",-1)
            );
        }else {
            referee = this.getOne(new QueryWrapper<MatchReferee>()
                    .eq("match_id", matchRefereeForm.getMatchId())
                    .eq("match_admin_id", matchRefereeForm.getAdminId())
                    .eq("referee_id",-1)
            );
        }

        if (referee == null) {
            referee = new MatchReferee();
            referee.setMatchId(matchRefereeForm.getMatchId());
            if (matchRefereeForm.getMatchId() != null) {
                referee.setMatchAdminId(matchRefereeForm.getAdminId());
            }else {
                referee.setMatchAdminId(SecurityUtils.getCurrentUserId(request));
            }
            referee.setRefereeId(sysUser.getId());
            baseMapper.insert(referee);
        } else {
            referee.setRefereeId(sysUser.getId());
            baseMapper.updateById(referee);
        }
    }

    @Transactional
    @Override
    public void delete(Integer[] ids) {
        List<MatchReferee> matchRefereeList = baseMapper.selectBatchIds(Arrays.asList(ids));
        if (CollectionUtils.isEmpty(matchRefereeList)) {
            return;
        }

        List<Integer> refereeIds = matchRefereeList.stream().map(MatchReferee::getRefereeId).collect(Collectors.toList());
        baseMapper.deleteBatchIds(Arrays.asList(ids));
        sysUserService.removeByIds(refereeIds);
    }

    @Override
    public Integer selectRefereeCount(Integer matchId) {
        return baseMapper.selectRefereeCount(matchId);
    }

	@Override
	public void updateCheckTime(Integer id) {
		MatchReferee referee = baseMapper.selectById(id);
        referee.setCheckTime(new Date());;
        baseMapper.updateById(referee);
	}
}
